﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Adex;
using Telerik.Web.UI;
using MediaMonitor.Common;

namespace MediaMonitor.Users
{
    public partial class SearchVideo : System.Web.UI.Page
    {
        DateTime sDate = DateTime.MinValue, eDate = DateTime.MinValue;
        private int mediaTypeCode = 0;
        private string mediaCodes = "";
        string mediaTypeName = "";
        string AdvertiserName = "";
        string AdvertiserMake = "";
        string NewsTitle = "";
        string Calification = "";
        string reportUrl = "";
        bool TitleNews;
        bool Summary;
        bool Text;
        string mediaTypeNameMake = "";
        string TV = "";
        string Radio = "";
        string Internet = "";
        string Press = "";
        string InMedia = "";
        string Code = "";
        string days = "";
        string Importancia = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Utility.AddDateControlToPage(this.Page);
                SetCalendarDates();
                FillMediaTypes();
            }
        }
        private void SetCalendarDates()
        {


            DataTable dt = TblNewsAlert.Query().DISTINCT().SetSelectList("RFDate").ExecuteDataSet().Tables[0];


            foreach (DataRow dr in dt.Rows)
            {

                RadCalendarDay d1 = new RadCalendarDay(calS);
                d1.Date = Utility.ToDateTime(dr["RFDate"]);
                d1.IsDisabled = false;
                d1.IsSelectable = true;

                d1.ItemStyle.BackColor = System.Drawing.Color.PowderBlue;
                d1.ItemStyle.ForeColor = System.Drawing.Color.White;

                calS.SpecialDays.Add(d1);

                RadCalendarDay d2 = new RadCalendarDay(calE);
                d2.Date = Utility.ToDateTime(dr["RFDate"]);
                d2.IsDisabled = false;
                d2.IsSelectable = true;

                d2.ItemStyle.BackColor = System.Drawing.Color.PowderBlue;
                d2.ItemStyle.ForeColor = System.Drawing.Color.White;


                calE.SpecialDays.Add(d2);
            }



        }
        private void FillMediaTypes()
        {
            ReportFuncs objReportFuncs = new ReportFuncs();
            DataTable dt = objReportFuncs.GETMediaTypeNameFORNewsAlert();
            chkNewsMediaType.DataSource = dt;
            chkNewsMediaType.DataBind();

        }
        protected void chkNewsMediaType_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (chkNewsMediaType.SelectedItem != null)
            {
                FillMedia(Utility.ToInt(chkNewsMediaType.SelectedValue));

                if (!chkAllMedia.Checked)
                    chkAllMedia.Checked = true;

                chkAllMedia_Changed(sender, e);
                //FillAdvertisers();
             
            }
        }
        private void FillMedia(int MediaTypeCode)
        {
            ReportFuncs objReportFuncs = new ReportFuncs();
            DataTable dt = objReportFuncs.GETMediaNameFORNewsAlert(MediaTypeCode);
            lstMedia.DataSource = dt;
            lstMedia.DataBind();
        }
        protected void chkAllMedia_Changed(object sender, EventArgs e)
        {
            if (!chkAllMedia.Checked)
                panelMediaList.Visible = true;
            else
                panelMediaList.Visible = false;
            pnlUpdate_Media1.Update();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
             bool tmp1 = DateTime.TryParse(calS.SelectedDate.ToShortDateString(), out sDate);
            bool tmp2 = DateTime.TryParse(calE.SelectedDate.ToShortDateString(), out eDate);

            if (sDate < Utility.ToDateTime("01/01/2000"))         //Calendar Start Date
                sDate = Utility.ToDateTime("");

            if (eDate < Utility.ToDateTime("01/01/2000"))        //Calendar End Date
                eDate = Utility.ToDateTime("01/01/2000");

            //mediaTypeCode = Utility.ToInt(lstNewsMediaType.SelectedValue);
            if (!chkMediaAll.Checked)
            {
                if (chkNewsMediaType.SelectedItem != null)
                {
                    foreach (System.Web.UI.WebControls.ListItem objItem in chkNewsMediaType.Items)
                    {
                        if (objItem.Selected)
                        {
                            mediaTypeName += objItem.Text + ",";
                            mediaTypeNameMake += "'" + objItem.Text + "',";
                        }
                    }
                    mediaTypeName = mediaTypeName.TrimEnd(',');
                    mediaTypeNameMake = mediaTypeNameMake.TrimEnd(',');
                }
                else
                {
                    string script = "alert(\"Please select atleast one News media type \");";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
                    return;
                }

            }
            else
            {
                mediaTypeName = "";
                mediaTypeNameMake = "";
            }
            if (mediaTypeName != "")
            {
                foreach (System.Web.UI.WebControls.ListItem itm in lstMedia.Items)
                {
                    if (itm.Selected)
                        mediaCodes += itm.Value + ",";
                }
                mediaCodes = mediaCodes.TrimEnd(',');
            }
            else
                mediaCodes = "";




            DataTable dt = SPs.GetReportVideoNewsAlert(sDate.ToShortDateString() + " 00:00:00:000", eDate.ToShortDateString() + " 23:59:59:999", txtNewsTitle.Text.ToString(), mediaTypeNameMake, mediaCodes).GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
            {
                radGrid.DataSource = dt;
                radGrid.DataBind();
                pnlSearch.Visible = false;
                pnlResult.Visible = true;
            }
            else
            {
                pnlResult.Visible = false;
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);

            }
        }
    }
}